package com.teama4.shop.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.teama4.shop.domain.Commodity;
import com.teama4.shop.domain.CommoditySpecification;
import com.teama4.shop.domain.SearchCommodity;
import com.teama4.shop.mapper.SearchMapperExt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class SearchServiceImplExt implements SearchServiceExt{

    @Autowired
    private SearchMapperExt searchMapperExt;

    @Override
    public SearchCommodity searchByName(String name, String sort, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        String name2 = name+"%";
        String name3 = "%"+name;
        String name4 = "%"+name+"%";
        List<Commodity> commodities = searchMapperExt.searchByName(name,name2,name3,name4,sort);
        return new SearchCommodity(((Page)commodities).getTotal(),commodities);
    }

    @Override
    public SearchCommodity searchedByBuyCount(String name, String sort, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        String name2 = name+"%";
        String name3 = "%"+name;
        String name4 = "%"+name+"%";
        List<Commodity> commodities = searchMapperExt.searchedByBuyCount(name,name2,name3,name4,sort);
        return new SearchCommodity(((Page)commodities).getTotal(),commodities);
    }

    @Override
    public SearchCommodity searchedByPrice(String name, String sort, int pageNum, int pageSize) {

        PageHelper.startPage(pageNum,pageSize);
        String name2 = name+"%";
        String name3 = "%"+name;
        String name4 = "%"+name+"%";
        List<Commodity> commodities = searchMapperExt.searchedByPrice(name,name2,name3,name4,sort);
        return new SearchCommodity(((Page)commodities).getTotal(),commodities);
    }

    @Override
    public SearchCommodity searchedByViewCount(String name, String sort, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        String name2 = name+"%";
        String name3 = "%"+name;
        String name4 = "%"+name+"%";
        List<Commodity> commodities = searchMapperExt.searchedByViewCount(name,name2,name3,name4,sort);
        return new SearchCommodity(((Page)commodities).getTotal(),commodities);
    }

    @Override
    public SearchCommodity searchedByMaxMinPrice(String name, Double maxPrice, Double minPrice,  int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        String name2 = name+"%";
        String name3 = "%"+name;
        String name4 = "%"+name+"%";
        List<Commodity> commodities = searchMapperExt.searchedByMaxMinPrice(name,name2,name3,name4,maxPrice,minPrice);
        return new SearchCommodity(((Page)commodities).getTotal(),commodities);
    }
}
